<script>
	export default defineComponent({
		setup() {
			onShow(() => {});

			onHide(() => {});

			onLaunch(() => {
				// 启动心跳
				keepAliveTimer();
				
				// 设置保持唤醒
				setDeviceIsConstantlyOn();
				
				// 申请忽略电池优化（Android）
				requestIgnoreBatteryOptimization();
			});

			function keepAliveTimer() {
				setInterval(() => {
					console.log('心跳:', new Date().toLocaleTimeString());
				}, 10000);
			}

			function setDeviceIsConstantlyOn() {
				if (plus.device) {
					plus.device.setWakelock(true);
					console.log('已设置设备常亮');
				}
			}

			function requestIgnoreBatteryOptimization() {
				if (!plus.os.name.toLowerCase().includes('android')) return;
				try {
					const main = plus.android.runtimeMainActivity();
					const Intent = plus.android.importClass('android.content.Intent');
					const Settings = plus.android.importClass('android.provider.Settings');
					const Uri = plus.android.importClass('android.net.Uri');
					const intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
					const uri = Uri.parse('package:' + main.getPackageName());
					intent.setData(uri);
					main.startActivity(intent);
					console.log('已请求忽略电池优化');
				} catch (e) {
					console.error('忽略电池优化请求失败:', e);
				}
			}
		},
	});
</script>